//
//  PageCollectionCell.m
//  IT爱好者
//
//  Created by dllo on 15/10/14.
//  Copyright © 2015年 Fan. All rights reserved.
//

#import "PageCollectionCell.h"
#import "Header.h"
#import "HotListTableViewCell.h"
#import "CFNetworkFromAFNetworking.h"
#import "HotArticleModel.h"
#import "MJRefresh.h"

@implementation PageCollectionCell

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self creatView];
        self.dataSourceArray = [NSMutableArray array];
        _page = 1;
        _totlePage = 1;
        _HUD = [MBProgressHUD showHUDAddedTo:self.contentView animated:YES];
        _HUD.backgroundColor = [UIColor clearColor];
        _HUD.labelText = @"正在加载";
        _HUD.detailsLabelText = @"请耐心等待";
                              
        [self getdataSource];
        
    }
    return self;
    
}

- (void)creatView
{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - (49 + 64)) style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.showsHorizontalScrollIndicator = NO;
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.backgroundColor = BackgroundColor;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.contentView addSubview:_tableView];
    
#pragma mark MJ刷新
    _tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _page = 1;
        [self getdataSource];
    }];
    _tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        _page++;
        [self getdataSource];
    }];
    
}
#pragma mark 关于tableView
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSourceArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    HotListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[HotListTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    
    cell.hotAM = self.dataSourceArray[indexPath.row];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
#warning 判断,无图 imageH = 0; 有图= 115
    NSInteger Hempty = (15 + 10 + 15 + 15 + 10) * FitHeight;
    //自适应高度
    NSDictionary *dicTitle = [NSDictionary dictionaryWithObjectsAndKeys:contentTextSize, NSFontAttributeName, nil];
    CGRect recttitle = [[self.dataSourceArray[indexPath.row] title] boundingRectWithSize:CGSizeMake(ScreenWidth - 50 * FitWidth, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:dicTitle context:nil];
    
    NSInteger Hword = recttitle.size.height + ( 20 + 10 + 10) * FitHeight;
    NSInteger HpicImage = 80 * FitHeight;
    //判断数据是否有图片
    NSInteger n = 0;
    for (NSString *str in [self.dataSourceArray[indexPath.row] pic_url]) {
        if (![str isEqualToString:@""]) {
            n ++;
        }
    }
    if (n >= 1) {
        //有图 返回高度加图的高度
        return Hempty + Hword + HpicImage;
    }else return Hempty + Hword;
}

#pragma mark 让代理执行传值并push到web页面的方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [_tableView deselectRowAtIndexPath:indexPath animated:NO];
    [self.delegate passValueAndPushTuWebViewByHotArticleModel:_dataSourceArray[indexPath.row]];
}

#pragma mark 获取数据
- (void)getdataSource
{
    if (_page <= _totlePage) {
        NSString *urlString = [NSString stringWithFormat:@"http://lib.wap.zol.com.cn/bbs/ios/getHotBook.php?page=%ld&userid=&vs=430", _page];
        [CFNetworkFromAFNetworking askObjectByURLString:urlString BodyString:nil WithObjectBlock:^(id object) {
            self.totlePage = [[object objectForKey:@"totalpage"] integerValue];
            NSArray *array = [object objectForKey:@"hotList"];
            if (_page == 1) {
                self.dataSourceArray = [NSMutableArray array];
            }
            for (NSDictionary *dic in array) {
                HotArticleModel *HAModel = [[HotArticleModel alloc]init];
                [HAModel setValuesForKeysWithDictionary:dic];
                [self.dataSourceArray addObject:HAModel];
            }
            [_tableView reloadData];
            [_tableView.header endRefreshing];
            [_tableView.footer endRefreshing];
#pragma mark 用这个属性比对 removefrom...可以避免每次刷新要判断有无_HUD
            [_HUD hide:YES];
        }];

    }else{
        [_tableView.footer noticeNoMoreData];
    }
    
}



















/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
